package com.lzh.controller;


import com.github.pagehelper.PageInfo;
import com.lzh.pojo.CE;
import com.lzh.pojo.College;
import com.lzh.pojo.Exam;
import com.lzh.service.CEService;
import com.lzh.service.CollegeService;
import com.lzh.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Reborn
 * @since 2020-06-15
 */
@Controller
@RequestMapping("/exam")
public class ExamController {

    @Autowired
    CollegeService collegeService;

    @Autowired
    CEService ceService;

    @Autowired
    ExamService examService;

    @RequestMapping("/list")
    public String queryExam(@RequestParam(value = "subjects",defaultValue = "null") String subjects,@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
            Model model){
        PageInfo<Exam> exams = examService.queryExamPage(pageNum,pageSize);
        model.addAttribute("pageInfoL",exams);
        model.addAttribute("type","queryAll");
        if (!subjects.equals("null")){
            PageInfo<Exam> examPageInfo = examService.queryExamBySuPage(subjects, pageNum, pageSize);
            model.addAttribute("pageInfoL",examPageInfo);
            model.addAttribute("type","querySu");
            model.addAttribute("su",subjects);
        }
        queryCo(model);
        return "exam/exams";
    }


    @PostMapping("/insert")
    public String insertExam(Exam exam,Model model){
        examService.insertExam(exam);
        model.addAttribute("bj","添加");
        return "exam/exams";
    }

    @RequestMapping("/delete")
    public String deleteExam(int id,Model model){
        List<College> colleges = collegeService.QueryCollege();
        model.addAttribute("colleges",colleges);
        Exam exam = examService.queryOneExam(id);
        model.addAttribute("bj","删除");
        model.addAttribute("exam",exam);
        ceService.deleteCEByEid(id);
        examService.deleteExam(id);
        return "exam/exams";
    }

    @RequestMapping("/update")
    public String updateExam(Exam exam,Model model){
        examService.updateExam(exam);
        System.out.println(exam);
        model.addAttribute("bj","修改");
        model.addAttribute("exam",exam);
        List<College> colleges = collegeService.QueryCollege();
        model.addAttribute("colleges",colleges);
        return "exam/exams";
    }

    //查询所有colleges
    @RequestMapping("/colleges")
    public String queryCo(Model model){
        List<College> colleges = collegeService.QueryCollege();
        model.addAttribute("colleges",colleges);
        return "exam/exams";
    }

    //查询单个学院的考试
    @RequestMapping("/queryCE")
    public String queryCE(int id,Model model,
                          @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        queryCo(model);
        List<Integer> ces = ceService.queryEByC(id);
        PageInfo<Exam> examPageInfo = examService.queryEByCPage(ces, pageNum, pageSize);
        model.addAttribute("pageInfoL",examPageInfo);
        model.addAttribute("id",id);
        model.addAttribute("type","queryCE");
        return "exam/exams";
    }



}

